﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using sconSDK;

namespace sconApp
{
    public partial class ServoCtl : UserControl
    {
        public ServoCtl()
        {
            InitializeComponent();
        }

        private Scon m_scon = null;
        private Servo m_servo = null;

        public Servo Servo
        {
            get { return m_servo; }
        }

        public void Init(Scon scon, int servoId)
        {
            if (scon != null
                && scon.IsConnected)
            {
                label1.Text = servoId.ToString();
                m_scon = scon;
                m_servo = new Servo(m_scon, servoId);
                m_servo.PositionChanged += new EventHandler<ServoParams>(servo_PositionChanged);

                trackBar1.Minimum = 6000;
                trackBar1.Maximum = 22500;
                trackBar1.ValueChanged += new EventHandler(trackBar1_ValueChanged);

                textBox1.Text = m_servo.Value.ToString();
                trackBar1.Value = m_servo.Value;
            }
        }

        void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            if (m_servo != null)
            {
                m_servo.Value = trackBar1.Value;
            }
        }

        void servo_PositionChanged(object sender, ServoParams e)
        {
            textBox1.Text = e.Position.ToString();
            trackBar1.Value = m_servo.Value;
            textBox1.Refresh();
            trackBar1.Refresh();
        }
    }
}
